<template>
    <el-select v-model="managementOffice" v-bind="$attrs" style="width: 100%" :placeholder="placeholder" @change="change">
        <el-option v-for="item in managementOfficeOptions" :key="item.value" :label="item.label" :value="isLabel ? item.label : item.value" />
    </el-select>
</template>

<script>
import { ManagementOfficeModel } from '@/models'

// 资产管理的公路
export default {
    name: 'AssetsManagementOfficeSelect',
    props: {
        value: {
            type: [Number, String],
            default: null
        },
        placeholder: {
            type: String,
            default: '请选择'
        },
        isLabel: {
            type: Boolean,
            default: false
        },
        option: {
            type: Object,
            default: () => {
                return {
                    label: 'name',
                    value: 'code'
                }
            }
        }
    },
    data() {
        return {
            model: new ManagementOfficeModel(),
            managementOffice: this.value,
            managementOfficeOptions: []
        }
    },
    watch: {
        value(newValue) {
            this.managementOffice = newValue
        }
    },
    created() {
        this.getList()
    },
    methods: {
        change(val) {
            this.$emit('update:value', val)
            const value = this.isLabel ? 'name' : 'code'
            const current = this.managementOfficeOptions.find(item => item[value] === val) || {}
            this.$emit('update:name', current.name)
            this.$emit('change', val, this.managementOfficeOptions)
        },
        getList() {
            this.model.getList({}, { pageSize: 10000 }).then(res => {
                this.managementOfficeOptions = (res.list || []).map(item => {
                    return {
                        label: item[this.option.label],
                        value: item[this.option.value],
                        code: item.code
                    }
                })
            })
        }
    }
}
</script>

<style lang="scss" scoped></style>
